<?php

class OcheredController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
    	 public function filters()
	{
		return array(
			'accessControl',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
			      'actions' => array('login'),
			      'users' => array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('index'),
				'roles'=>array('admin'),
			),
			array('deny',
			      'users' => array('*'),
			),
		);
	}

	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	public function actionIndex()
	{
        $model = Ochered::model()->findAll();
        $data = 'date_reg;fio_children;fio_parent;address;phone;birthday;number_ag;work_f;work_m;lgoty;email;
';
        foreach($model as $item){
            $data.=date('d.m.Y H:i:s',strtotime($item->date_reg)).';'.$item->fio_children.';'.$item->fio_parent.';'.$item->address.';'.$item->phone
            .';'.date('d.m.Y',strtotime($item->birthday)).';'.$item->number_ag.';'.$item->work_f.';'.$item->work_m.';'.$item->lgoty
            .';'.$item->email.';
';
        }
        header('Content-Type: text/plain');
        header('Content-Disposition: attachment; filename="Выгрузка_'.date('d.m.Y H:i:s').'.csv"');
        echo iconv('UTF-8','cp1251',$data);
        Yii::app()->end();
	}


    private function uniqueName($fname)
	{
		$ext = end(explode(".",$fname));
		return md5(uniqid()).'.'.$ext;
	}


}